package com.bcx.crm.java.annotation.autowiredResource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Created by ys on 2018/3/15.
 *
 * @Resource 与 @Autowired 注解的区别
 * 共同点：一旦指定按照名称查找，找不到就会报错
 */
@Service
public class ClassBeanService {
    /**
     * 属于J2EE的
     *Resource 1.默认按照名称(name)找bean,如果指定名称就只能按照名称找了
     *         2.找不到则按照类型(对象IClassBean)找,如果找到多个实现类则报错
     */
    @Resource(name = "classBeanImpl1",type = com.bcx.crm.java.annotation.autowiredResource.ClassBeanImpl1.class)
    private IClassBean iClassBean; // {iClassBean} 为默认查找的bean名称
    public String getResultByResource(){
        String name = iClassBean.getName("resource");
        System.out.println(name);
        return name;
    }

    /**
     * Autowired 1.默认按照类型去找(对象IClassBean2),如果找到多个会报错
     *           2.如果需要指定名称找,则需要Qualifier注解
     */
    @Autowired
    @Qualifier(value = "classBeanImpl2")
    private IClassBean2 iClassBean2;  // {iClassBean2} 为默认查找的bean名称
    public String getResultByAutowired(){
        String name = iClassBean2.getName("resource");
        System.out.println(name);
        return name;
    }

}
